package cn.bobmao.exts.example.service.user;

import cn.bobmao.exts.example.domain.user.Account;
import cn.bobmao.exts.example.mapper.user.AccountMapper;
import cn.bobmao.exts.example.model.user.AccountModel;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountService {

    private final AccountMapper mapper;
    private final ModelMapper modelMapper;

    public AccountService(AccountMapper mapper, ModelMapper modelMapper) {
        this.mapper = mapper;
        this.modelMapper = modelMapper;
    }

    public List<AccountModel> findAll() {
        List<Account> accounts = mapper.findAll();
        return modelMapper.map(accounts, new TypeToken<List<AccountModel>>() {
        }.getRawType());
    }

    public AccountModel find(String id) {
        Account account = mapper.find(id);
        if (account == null) {
            return null;
        }
        return modelMapper.map(account, AccountModel.class);
    }


}
